#include "DaoChapter.h"
#include "SqlDef.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include <exception/NpException.h>
#include <log/NpLogger.h>

using namespace chapterInfo;
using namespace std;

void DaoChapter::fillChapter(const MYSQL_ROW &row, chapterInfo::ChapterInfo &chapter)
{
  chapter.articleid.assign(row[chapter_articleid]);
  chapter.articlename.assign(row[chapter_articlename]);
  chapter.attachment.assign(row[chapter_attachment]);
  chapter.chapterid.assign(row[chapter_chapterid]);
  chapter.chaptername.assign(row[chapter_chaptername]);
  chapter.chapterorder.assign(row[chapter_chapterorder]);
  chapter.chaptertype = (strlen(row[chapter_chaptertype]) == 0) ? '0' : row[chapter_chaptertype][0];
  chapter.display = (strlen(row[chapter_display]) == 0) ? '0' : row[chapter_display][0];
  chapter.isvip = (strlen(row[chapter_isvip]) == 0) ? '0' : row[chapter_isvip][0];
  chapter.lastupdate.assign(row[chapter_lastupdate]);
  chapter.postdate.assign(row[chapter_postdate]);
  chapter.poster.assign(row[chapter_poster]);
  chapter.posterid.assign(row[chapter_posterid]);
  chapter.power = (strlen(row[chapter_power]) == 0) ? '0' : row[chapter_power][0];
  chapter.salenum.assign(row[chapter_salenum]);
  chapter.saleprice.assign(row[chapter_saleprice]);
  chapter.siteid.assign(row[chapter_siteid]);
  chapter.size.assign(row[chapter_size]);
  chapter.totalcost.assign(row[chapter_totalcost]);
  chapter.volumeid.assign(row[chapter_volumeid]);
  chapter.ctype.assign(row[chapter_ctype]);
  chapter.isnew = '0';
}

int DaoChapter::traverseResult(const MYSQL_RES *res, chapterInfo::ChapterInfo *chapterList)
{
  static MYSQL_ROW row;
  int count= 0;
  while ((row = mysql_fetch_row(const_cast<MYSQL_RES*>(res))) != NULL)
  {
    fillChapter(row, chapterList[count]);
    ++count;
  }
  return count;
}

int DaoChapter::getChapterList(const articleInfo::ArticleInfo &article, chapterInfo::ChapterInfo *chapterList)
{
  static char sql[SQL_LENGTH];
  sprintf(sql, SQL_CHAPTER_SELECT, article.articleid.c_str());
  TRAVERSE_AND_RETURN_ROWS(chapterList);
}

int DaoChapter::getChapterByTime(const std::string &lastupdate, const int max, chapterInfo::ChapterInfo *chapterList)
{
  static char sql[SQL_LENGTH];
  sprintf(sql, SQL_CHAPTER_SELECT_TIME, lastupdate.c_str(), max);
  TRAVERSE_AND_RETURN_ROWS(chapterList);
}

void DaoChapter::getChapterMaxID(std::string &maxId)
{
  maxId = "";
  static char sql[SQL_LENGTH];
  sprintf(sql, SQL_CHAPTER_MAXID);
  MYSQL_RES* res = doQuery(sql);
  static MYSQL_ROW row;
  char tmp[12]={0};
  while((row = mysql_fetch_row(res)) != 0)
  {
    if(row[0]!=NULL && strlen(row[0])>0)
    {
      sprintf(tmp,"%d",atoi(row[0])+1);
      maxId.assign(tmp);
    }
  }
  if(maxId.length() == 0)
  {
    maxId = "1";
  }
  mysql_free_result(res);

}

unsigned int DaoChapter::getChapterMaxID()
{
  unsigned int maxID =1;
  static char sql[SQL_LENGTH];
  sprintf(sql, SQL_CHAPTER_MAXID);
  MYSQL_RES* res = doQuery(sql);
  static MYSQL_ROW row;
  while((row = mysql_fetch_row(res)) != 0)
  {
    if(row[0]!=NULL && strlen(row[0])>0)
    {
      maxID=atoi(row[0])+1;
    }
  }
  mysql_free_result(res);
  return maxID;
}

void DaoChapter::addChapter(const chapterInfo::ChapterInfo &chapter)
{
  static char sql[SQL_LENGTH];
  sprintf(sql, SQL_CHAPTER_INSERT, chapter.chapterid.c_str(),chapter.siteid.c_str(),
          chapter.articleid.c_str(),chapter.articlename.c_str(),chapter.volumeid.c_str(),
          chapter.posterid.c_str(),chapter.poster.c_str(),chapter.postdate.c_str(),
          chapter.lastupdate.c_str(),chapter.chaptername.c_str(), chapter.chapterorder.c_str(),
          chapter.size.c_str(),chapter.saleprice.c_str(),chapter.salenum.c_str(),chapter.totalcost.c_str(),
          chapter.attachment.c_str(),chapter.isvip,chapter.chaptertype,chapter.power,chapter.display,chapter.txtlink.c_str());
  doDml(sql);
}

void DaoChapter::addChapterCache(const chapterInfo::ChapterInfo &chapter)
{
  static char sql[SQL_LENGTH];
  sprintf(sql, SQL_CHAPTER_INSERT, chapter.chapterid.c_str(),chapter.siteid.c_str(),
          chapter.articleid.c_str(),chapter.articlename.c_str(),chapter.volumeid.c_str(),
          chapter.posterid.c_str(),chapter.poster.c_str(),chapter.postdate.c_str(),
          chapter.lastupdate.c_str(),chapter.chaptername.c_str(), chapter.chapterorder.c_str(),
          chapter.size.c_str(),chapter.saleprice.c_str(),chapter.salenum.c_str(),chapter.totalcost.c_str(),
          chapter.attachment.c_str(),chapter.isvip,chapter.chaptertype,chapter.power,chapter.display,chapter.txtlink.c_str());
  m_sql += sql;
}

void DaoChapter::updateChapterType(const chapterInfo::ChapterInfo& chapter)
{
  static char sql[SQL_LENGTH];
  sprintf(sql, SQL_CHAPTER_UPDATE_TYPE, chapter.ctype.c_str(), chapter.chapterid.c_str());
  doDml(sql);
}

bool DaoChapter::confirmCacheTran()
{
  int result = 0;
  result = mysql_real_query(m_conn, m_sql.c_str(), m_sql.length());
  MYSQL_RES* res;
  do
  {
    res = mysql_store_result( m_conn );
    mysql_free_result(res);
    res = NULL;
  }while(!mysql_next_result( m_conn ) );
  restCache();
  return !result;
}

